<template>
  <view class="menu">
    <view class="menuCon" @click="isShow = !isShow">
      <view>{{ getName }}</view>
      <i class="iconfont icon-arrow-down" :class="[getClass]"></i>
    </view>

    <view class="pop" v-show="isShow">
      <view
        class="item"
        v-for="item of menu"
        :key="item.id"
        :class="[getItemClass(item.id)]"
        @click="handle(item.id)"
        >{{ item.name }}</view
      >
    </view>
  </view>
</template>

<script>
export default {
  props: {
    index: {
      type: String,
      default: "",
    },

    menu: {
      type: Array,
      default() {
        return [];
      },
    },
  },

  computed: {
    getItemClass() {
      return (id) => {
        let { index } = this;

        return index === id ? "on" : "";
      };
    },

    getName() {
      let { index, menu } = this,
        item = menu.find((el) => el.id === index);

      return item ? item.name : "";
    },

    getClass() {
      return this.isShow ? "on" : "";
    },
  },

  data() {
    return {
      isShow: false,
    };
  },

  methods: {
    handle(id) {
      if (this.index === id) return;

      this.$emit("change", id);
    },
  },
};
</script>

<style lang="less" scoped>
.menu {
  position: relative;
  color: #262626;
  font-size: 28rpx;
}

.menuCon {
  display: flex;
  align-items: center;
  padding: 28rpx 0;

  .iconfont {
    display: inline-block;
    margin-left: 10rpx;
    transition: 0.3s all;
  }

  &.on .iconfont {
    transform: rotate(180deg);
  }
}

.pop {
  position: absolute;
  left: 0;
  top: 100%;
  max-width: 100%;
  z-index: 4;
  box-shadow: 0px 6px 12px rgba(0, 0, 0, 0.16);
  border-radius: 20rpx;
  background: #fff;

  .item {
    padding: 0 30rpx;
    height: 72rpx;
    line-height: 72rpx;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    border-bottom: 1px solid #f1f1f1;

    &.on {
      background: #ffe8e1;
      color: #ff714a;
    }
  }
}
</style>
